<?php
require_once 'util/class.constants.php';

// step1, check api
$result=array();
$api=$_GET['api'];
if(!$api) {
	$result['status']=Constants::$ERROR_NOT_EXIT;
	$result['msg']='';
	$result['data']='';
	echo json_encode($result);
	return;
}

$prefix = strtok($api, '.');
switch($prefix) {
	case 'relation':
		require_once 'service/class.relationservice.php';
		break;
	case 'user':
		require_once 'service/class.userservice.php';
		break;
	case 'notify':
		require_once 'service/class.notifyservice.php';
		break;
	case 'note':
		require_once 'service/class.noteservice.php';
		break;
	case 'taglist':
		require_once 'service/class.taglistservice.php';
		break;
	case 'discovery':
		require_once 'service/class.discoverylistservice.php';
		break;
	default:
		break;
}

$src=0;
$ip=isset($_SERVER[ "REMOTE_ADDR "]) ? $_SERVER[ "REMOTE_ADDR "] : '';

// TODO step2, check login

switch($api) {
	case 'relation.getfollowers': 
		{
			$service = new RelationService();
			$result = $service->getFollower(intval($_GET['uid']), intval($_GET['pageNumber']));
			break;
		}
	case 'relation.getfollowees':
		{
			$service = new RelationService();
			$result = $service->getFollowee(intval($_GET['uid']), intval($_GET['pageNumber']));
			break;
		}
	case 'relation.create':
		{
			$service = new RelationService();
			$result = $service->createFollowRelation(intval($_GET['uid']), intval($_GET['followerId']));
			break;
		}
	case 'relation.remove':
		{
			$service = new RelationService();
			$result = $service->destoryFollowRelation(intval($_GET['uid']), intval($_GET['followerId']));
			break;
		}
	case 'user.getbasicinfo':
		{
			$service = new UserService();
			$result = $service->getUserBasicInfo(intval($_GET['uid']));
			break;
		}
	case 'user.getbasicinfobynick':
		{
			$service = new UserService();
			$result = $service->getUserBasicInfoByName(urldecode($_GET['nick']));
			break;
		}
	case 'user.search':
		{
			$service = new UserService();
			$nickPattern = urldecode($_GET['nick']);
			$offset = intval($_GET['offset']);
			$limit = intval($_GET['limit']);
			$result = $service->searchUserByName($nickPattern, $offset, $limit);
			break;
		}
	case 'user.recommend':
		{
			$service = new UserService();
			$num = intval($_GET['num']);
			$result = $service->recommendUser($num);
			break;
		}
	case 'user.register':
		{
			$service = new UserService();
			$result = $service->register(urldecode($_GET['nick']), urldecode($_GET['email']), $_GET['password'], $src, $ip);
			break;
		}
	case 'user.login':
		{
			$service = new UserService();
			$result = $service->login(urldecode($_GET['email']), $_GET['password']);
			break;
		}
	case 'user.logout':
		{
			$service = new UserService();
			$result = $service->logout(intval($_GET['uid']), $_GET['skey']);
			break;
		}
	case 'user.advice':
		{
			$service = new UserService();
			$text=urldecode($_GET['text']);
			$result = $service->postAdvice(intval($_GET['uid']), $text);
			break;
		}
	case 'user.modifyprofile':
		{
			$service = new UserService();
			$result = $service->modifyProfile(intval($_GET['uid']), $_GET['nick'], $_GET['password'], 
						$_GET['birthday'], $_GET['profile']);
			break;
		}
	case 'notify.get':
		{
			$service = new NotifyService();
			$result = $service->getNotify(intval($_GET['uid']));
			break;
		}
	case 'notify.remove':
		{
			$service = new NotifyService();
			$uid=intval($_GET['uid']);
			$keys=split('_', $_GET['type']);
			$values=array();
			foreach ($keys as $key) {
				$values[$key]=0;
			}
			$result = $service->removeNotify($uid, $values);
			break;
		}
	case 'note.publish':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$note=urldecode($_GET['note']);
			$pics=$_GET['pics'];
			$tags=$_GET['tags'];
			$result=$service->publishNote($uid, $note, $pics, $tags, $src, $ip);
			break;
		}
	case 'note.forward':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$note=urldecode($_GET['note']);
			$replyNoteId=intval($_GET['replyNoteId']);
			$replyUserId=intval($_GET['replyUserId']);
			$result=$service->forwardNote($uid, $note, $replyNoteId, $replyUserId, $src, $ip);
			break;
		}
	case 'note.comment':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$note=urldecode($_GET['note']);
			$replyNoteId=intval($_GET['replyNoteId']);
			$replyUserId=intval($_GET['replyUserId']);
			$result=$service->commentNote($uid, $note, $replyNoteId, $replyUserId, $src, $ip);
			break;
		}
	case 'note.like':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$replyNoteId=intval($_GET['replyNoteId']);
			$replyUserId=intval($_GET['replyUserId']);
			$result=$service->likeNote($uid, $replyNoteId, $replyUserId, $src, $ip);
			break;
		}
	case 'note.sendprivatemsg':
		{
			$service = new NoteService();
			$fromUid=intval($_GET['fromUid']);
			$toUid=intval($_GET['toUid']);
			$message=urldecode($_GET['message']);
			$result=$service->sendPrivateMessage($fromUid, $toUid, $message, $src, $ip);
			break;
		}
	case 'note.getfriendstimeline':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$pageIndex=intval($_GET['pageIndex']);
			$sinceNoteId=intval($_GET['sinceNoteId']);
			$result=$service->getFriendsTimeline($uid, $pageIndex, $sinceNoteId);
			break;
		}
	case 'note.getpublictimeline': 
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$pageIndex=intval($_GET['pageIndex']);
			$sinceNoteId=intval($_GET['sinceNoteId']);
			$type=intval($_GET['type']);
			$result=$service->getPublicTimeline($uid, $pageNum, $sinceNoteId, $type);
			break;
		}
	case 'note.getuserpublictimeline':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$quid=intval($_GET['quid']);
			$pageIndex=intval($_GET['pageIndex']);
			$sinceNoteId=intval($_GET['sinceNoteId']);
			$result=$service->getUserPublicTimeline($uid, $quid, $pageIndex, $sinceNoteId);
			break;
		}
	case 'note.getuseralltimeline':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$pageIndex=intval($_GET['pageIndex']);
			$sinceTimestamp=intval($_GET['sinceTimestamp']);
			$result=$service->getUserAllTimeline($uid, $pageIndex, $sinceTimestamp);
			break;
		}
	case 'note.getcomments':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$noteId=intval($_GET['noteId']);
			$pageIndex=intval($_GET['pageIndex']);
			$sinceCommentId=intval($_GET['sinceCommentId']);
			$result=$service->getNoteComments($noteId, $pageIndex, $sinceCommentId);
			break;
		}
	case 'note.getnote':
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$noteId=intval($_GET['noteId']);
			$result=$service->getDisplayedNote($noteId);
			break;
		}
	case 'note.getprivatemessages': 
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$pageIndex=intval($_GET['pageIndex']);
			$sinceMessageId=intval($_GET['sinceMessageId']);
			$result=$service->getPrivateMessages($uid, $pageIndex, $sinceMessageId);
			break;
		}
	case 'note.getrecentprivaterecord': 
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$result=$service->getRecentPrivateMessagesRecord($uid);
			break;
		}
	case 'note.getallprivaterecord': 
		{
			$service = new NoteService();
			$uid=intval($_GET['uid']);
			$otherUid=intval($_GET['ouid']);
			$result=$service->getAllPrivateMessagesRecord($uid, $otherUid);
			break;
		}
	case 'taglist.getlist':
		{
			$service = new TagListService();
			$uid=intval($_GET['uid']);
			$tagId=intval($_GET['tagId']);
			$pageIndex=intval($_GET['pageIndex']);
			$sinceNoteId=intval($_GET['sinceNoteId']);
			$result=$service->getList($tagId, $pageIndex, $sinceNoteId);
			break;
		}
	case 'discovery.getlist':
		{
			$service = new DiscoveryListService();
			$uid=intval($_GET['uid']);
			$pageIndex=intval($_GET['pageIndex']);
			$sinceNoteIdList=intval($_GET['sinceNoteIdList']);
			$result=$service->getlist($sinceNoteIdList, $pageIndex);
			break;
		}
	default:
		break;
}

echo json_encode($result);
